# -*- coding: utf-8 -*-
__author__ = 'gerry'

import sys


for i in enumerate(range(1,10)):
    print(i)
#name = input("input your name")

name = "alex,eric,jack"
name = name.split(',')
print name[1]




f = file("myFile.txt",'r') #读取文件

#print f.readline() #打印第一行
#print f.readlines()#把整个文件变成一个列表


for line in f.readlines():
    print line,
f.close()

#write
f1 = file("test.txt","w") #写的时候必须要加格式限定
f1.write("this is the second line\n")
f1.write(("this is the third line\n"))

f1.close()


# append

f2 = file('test.txt','a')
f2.write("this is the appending apart\n")
f2.write("this is the appending apart2")
f2.close()


# read and write :r+ w+

f3 = file("test.txt",'r+')
#f3.write("first line\n")
#f3.write("second line\n")
#f3.write("third line\n")

f3.flush()#把内存的数据写到硬盘中

#print f3.readline()
#print f3.readline()

for line in f3.xreadlines():
    print(line)

f = file("myFile.txt")
for line in f.readlines():
    print(line,'-----------')
    line = line.strip('\n').split(':')
    print(line)



#修改文件

if '-r' in sys.argv:
    re_argv_pos = sys.argv.index('-r')
    find_str = sys.argv[re_argv_pos +1]
    new_str = sys.argv[re_argv_pos +2]



f = file('myFile.txt','r+')
#find_str = 'root'
#new_str = 'ROOT'

while True:
    line = f.readline()
    if find_str in line:
        last_line_pos = f.tell()-len(line)
        f.seek(last_line_pos)
        new_line = line.replace(find_str,new_str)
        f.write(new_line)

        print line
        break


f.close()




















